﻿using LitJson;
using System;
using System.IO;
using System.Text;
using UnityEngine;

namespace BToolkit.ExeUtils
{
    public class Config
    {
        private static Config _instance;
        public static Config Instance { get { return _instance ?? (_instance = new Config()); } }

        //通用参数
        public bool UseConfigScreenSize = false;
        public int ScreenW = 1920;
        public int ScreenH = 1080;
        public int SelectScreen = 1;
        public bool ScreensaverDefaultShow = false;
        public int ScreensaverWait = 0;
        public bool VideoHardwareDecode = true;

        //项目参数
        public float AlphaAnimMin = 0, AlphaAnimSpeed = 5;
        public string ContentSize = "745x420";
        public float LineSize = 5;
        public string LineColor1 = "#00F5FF";
        public string LineColor2 = "#FFF900";

        private Config()
        {
            string path = MainController.resPath + "/config.txt";
            if (!File.Exists(path))
            {
                Debug.LogError("文件不存在[" + path + "]");
                return;
            }
            byte[] bytes = File.ReadAllBytes(path);
            string jsonStr = Encoding.UTF8.GetString(bytes);
            JsonData json;
            try
            {
                json = JsonMapper.ToObject(jsonStr);
            }
            catch
            {
                jsonStr = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
                json = JsonMapper.ToObject(jsonStr);
            }

            try
            {
                if (json.Contains("UseConfigScreenSize")) UseConfigScreenSize = (bool)json["UseConfigScreenSize"];
                if (json.Contains("ScreenW")) ScreenW = (int)json["ScreenW"];
                if (json.Contains("ScreenH")) ScreenH = (int)json["ScreenH"];
                if (json.Contains("SelectScreen")) SelectScreen = (int)json["SelectScreen"];
                if (json.Contains("ScreensaverDefaultShow")) ScreensaverDefaultShow = (bool)json["ScreensaverDefaultShow"];
                if (json.Contains("ScreensaverWait")) ScreensaverWait = (int)json["ScreensaverWait"];
                if (json.Contains("VideoHardwareDecode")) VideoHardwareDecode = (bool)json["VideoHardwareDecode"];

                if (json.Contains("AlphaAnimMin")) AlphaAnimMin = (float)json["AlphaAnimMin"];
                if (json.Contains("AlphaAnimSpeed")) AlphaAnimSpeed = (float)json["AlphaAnimSpeed"];
                if (json.Contains("ContentSize")) ContentSize = (string)json["ContentSize"];
                if (json.Contains("LineSize")) LineSize = (float)json["LineSize"];
                if (json.Contains("LineColor1")) LineColor1 = (string)json["LineColor1"];
                if (json.Contains("LineColor2")) LineColor2 = (string)json["LineColor2"];
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
    }
}